#!/usr/bin/python

import gobject;
gobject.threads_init()
import gst;


#RADO

if __name__ == "__main__":
    # First create our pipeline
    pipe = gst.Pipeline("mypipe")

    # Create a software mixer with "Adder"
    adder = gst.element_factory_make("adder","audiomixer")
    pipe.add(adder)

    #Create microphone
    microphone = gst.element_factory_make("pulsesrc","microphone")
    microphone.set_property("device","alsa_input.pci-0000_02_02.0.analog-stereo")
    mic_volume = gst.element_factory_make("volume","mic_volume")
    mic_volume.set_property("volume", 1)
    mic_audioconvert = gst.element_factory_make("audioconvert","mic_audioconvert")

    pipe.add(microphone, mic_volume, mic_audioconvert)
    
    #Link the microphone to the mixer
    gst.element_link_many(microphone, mic_volume, mic_audioconvert, adder)
    
    #Create the music 
    source = gst.element_factory_make("filesrc", "file-source")
    source.set_property("location", "/mnt/c_drive/1.mp3")
    decoder = gst.element_factory_make("mad", "mp3-decoder")
    songaudioconvert = gst.element_factory_make("audioconvert", "songaudioconvert")
    volume_control = gst.element_factory_make("volume", "volume_control")
    volume_control.set_property("volume", 0.1)
    
    pipe.add(source, decoder, songaudioconvert, volume_control)
    
    #Link the music to the mixer
    gst.element_link_many(source, decoder, songaudioconvert, volume_control, adder)
    
    #Create splitter which will split the output  - for stream and output
    splitter = gst.element_factory_make("tee", "splitter")
    pipe.add(splitter)
    adder.link(splitter)
    
    #Create local output
    output = gst.element_factory_make("pulsesink", "audio_out")
    audioconvert = gst.element_factory_make("audioconvert","audioconvert")
    outputvolume = gst.element_factory_make("volume","outputvolume")
    outputvolume.set_property("volume", 1)
    pipe.add(audioconvert, output, outputvolume)
    
    #Create queue for the locl output
    directoutput = gst.element_factory_make("queue", "directoutput")
    pipe.add(directoutput)
    
    #Link splitter to the local output
    gst.element_link_many(splitter, directoutput, audioconvert, outputvolume, output)
    
    #Create radio ogg streamer
    vor = gst.element_factory_make("vorbisenc", "vorbisenc")
    oggmux = gst.element_factory_make("oggmux", "oggmux")
    shout2send = gst.element_factory_make("shout2send", "shout2send")
    
    shout2send.set_property("mount", "/stream.ogg")
    shout2send.set_property("port", 8000)
    shout2send.set_property("ip", "192.168.1.110")
    shout2send.set_property("password", "hackme")
    pipe.add(vor, oggmux, shout2send)
    
    #Link splitter to the radio stream output
    gst.element_link_many(splitter, vor, oggmux, shout2send)



    # Start the playback
    pipe.set_state(gst.STATE_PLAYING)
    
    
    
    
    
    
    #raw_input("Press <ENTER> to unlink.")
    #source3.unlink(decoder3)
    #source3.set_state(gst.STATE_PAUSED)
    #pipe.set_state(gst.STATE_PLAYING)
    
    
    raw_input("Press <ENTER> to link.")
    
    buzzer2=gst.element_factory_make("audiotestsrc","buzzer2")
    buzzer2.set_property("volume",0)
    pipe.add(buzzer2)
    buzzer2.link(adder)
    
    
    source3 = gst.element_factory_make("filesrc", "file-source3")
    source3.set_property("location", "/mnt/c_drive/brothers.mp3")
    decoder3 = gst.element_factory_make("mad", "mp3-decoder3")
    songaudioconvert3 = gst.element_factory_make("audioconvert", "songaudioconvert3")
    volume_control3 = gst.element_factory_make("volume", "volume_control3")
    volume_control3.set_property("volume", 0.5)
    
    pipe.add(source3, decoder3, songaudioconvert3, volume_control3)
    gst.element_link_many(source3, decoder3, songaudioconvert3, volume_control3)
    volume_control3.link(adder)
    
    volume_control.set_property("volume", 0)
    
    pipe.set_state(gst.STATE_PLAYING)
    
    
    
    raw_input("Press <ENTER> to link.")
    
    buzzer3=gst.element_factory_make("audiotestsrc","buzzer3")
    buzzer3.set_property("volume",0)
    pipe.add(buzzer3)
    buzzer3.link(adder)
    buzzer2.unlink(adder)
    
    source4 = gst.element_factory_make("filesrc", "file-source4")
    source4.set_property("location", "/mnt/c_drive/test.mp3")
    decoder4 = gst.element_factory_make("mad", "mp3-decoder4")
    songaudioconvert4 = gst.element_factory_make("audioconvert", "songaudioconvert4")
    volume_control4 = gst.element_factory_make("volume", "volume_control4")
    volume_control4.set_property("volume", 0.5)
    
    pipe.add(source4, decoder4, songaudioconvert4, volume_control4)
    gst.element_link_many(source4, decoder4, songaudioconvert4, volume_control4)
    volume_control4.link(adder)
    
    volume_control3.set_property("volume", 0)
    
    pipe.set_state(gst.STATE_PLAYING)
    
    
    
    
    raw_input("Press <ENTER> to EXIT.")
    
    
    
    def on_message(self, bus, message):
      t = message.type
      if t == gst.MESSAGE_EOS:
	self.player.set_state(gst.STATE_NULL)
	self.playmode = False
      elif t == gst.MESSAGE_ERROR:
	self.player.set_state(gst.STATE_NULL)
	err, debug = message.parse_error()
	print "Error: %s" % err, debug
	self.playmode = False

    
